<!DOCTYPE html>
<html lang="ch">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/list.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/urlUtil.js"></script>
    <style>
        
        .nav{
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 50px;
        }
        .container{
            padding-top: 80px;
            height: auto;
        }
        .container-right{
            width: auto;
        }
        .blog-pagnation-wrapper{
            height: 40px;
            margin: 16px 0;
            text-align: center;
        }
        .blog-pagnation-item{
            display: inline-block;
            padding: 8px;
            border: 1px solid #d0d0d5;
            color: #333;
        }
        .blog-pagnation-item:hover{
            background: #4e4eeb;
            color: #fff;
        }
        .blog-pagnation-item.actvie{
            background: #4e4eeb;
            color: #fff;
        }

    </style>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="myblog_list.html">个人主页</a>
        <a href="blog_add.html">写博客</a>
        <a href="login.html">登录</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 右侧内容详情 -->
        <div class="container-right" style="width: 100%;">
            <!-- 每一篇博客包含标题, 摘要, 时间 -->
            <div id="artListDiv">
                
            </div>
           
            <hr>
            <div class="blog-pagnation-wrapper">
                <button class="blog-pagnation-item" onclick="doFirst()">首页</button> 
                <button class="blog-pagnation-item" onclick="doBefore()">上一页</button>  
                <button class="blog-pagnation-item" onclick="doNext()">下一页</button>
                <button class="blog-pagnation-item" onclick="doLast()">末页</button>
                &nbsp;&nbsp;&nbsp;&nbsp;
                当前在第<span id="pIndex"></span>页，共<span id="AIndex"></span>页
            </div>
        </div>
    </div>
    <script>
        var pageSize = 3; // 每页文章数
        var pageIndex = 1; // 页面位置
        var pageTotal = 1; // 总页面数
        // 页面初始话
        function init() {
            // 参数校正
            pageIndex = getParams("index");
            if (pageIndex == null || pageIndex <= 0) {
                pageIndex = 1;
            }
            pageSize = getParams("size");
            if (pageSize == null || pageSize <= 0) {
                pageSize = 3;
            }
            jQuery("#pIndex").html(pageIndex);
            jQuery.ajax({
                url:"/article/get_page",
                type:"get",
                data:{
                    "index":pageIndex,
                    "size":pageSize
                },
                success: function(res) {
                    if (res.code == 200 && res.data != null) {
                        // 请求成功
                        var createHtml = "";
                        if (res.data.articles != null && res.data.articles.length > 0) {
                            // 说明有文章
                            pageTotal = res.data.total;
                            jQuery("#AIndex").html(pageTotal);
                            var articles = res.data.articles;
                            for (var i = 0; i < articles.length; i++) {
                                var article = articles[i];
                                createHtml += '<div class="blog" >';
                                createHtml += '<div class="title">'+article.title+'</div>';
                                createHtml += '<div class="date">'+article.createTime+'</div>';
                                createHtml += '<div class="desc">'+article.content+'</div>';
                                createHtml += '<a href="blog_content.html?aid='+
                                article.id+'" class="detail">查看全文 &gt;&gt;</a>';
                                createHtml += '</div>';
                            }
                        } else {
                            createHtml += '<h3 style="margin-top:20px;margin-left:20px;">暂无文章！</h3>';
                        }
                        jQuery("#artListDiv").html(createHtml);
                    } else {
                        alert("操作失败："+res.msg);
                    }
                }
            });
        }
        init();
        function doFirst() {
            // 判断是否已经在首页了
            if (pageIndex <= 1) {
                alert("已经在首页了！");
                return false;
            }
            location.href = "blog_list.html";
        }
         
        function doLast() {
            // 判断是否在最后一页
            if (pageIndex >= pageTotal) {
                alert("已经是最后一页了！");
                return false;
            }
            location.href = "blog_list.html?index=" + pageTotal;
        }

        function doBefore() {
            // 判断是否在首页
            // 判断是否已经在首页了
            if (pageIndex <= 1) {
                alert("没有上一页了！");
                return false;
            }
            location.href = "blog_list.html?index=" +(parseInt(pageIndex) - 1);
        }
        function doNext() {
            if (pageIndex >= pageTotal) {
                alert("没有下一页了！");
                return false;
            }
            location.href = "blog_list.html?index=" + (parseInt(pageIndex) + 1)
        }
    </script>
</body>
</html>